package com.testmiuu.club.annotation;

import org.springframework.util.StringUtils;
import sun.security.krb5.internal.crypto.Des;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ParseAnn {
    public static void main(String[] args) {
        //1.使用类加载器加载类(反射)
        try {
            Class c = Class.forName("com.testmiuu.club.annotation.Child");
            //2.找到类上面的注解
            //判断这个类上是否存在Description的注解
            boolean isExist = c.isAnnotationPresent(Description.class);
            if(isExist) {
                //拿到注解实例 需要强转
                Description d = (Description) c.getAnnotation(Description.class);
                System.out.println(d.value());
            }
            //4.找到方法上的注解
            //遍历所有的方法
            Method[] ms = c.getMethods();
            for (Method m : ms) {
                //指定拿Description的注解
                boolean isMExist = m.isAnnotationPresent(Description.class);
                if(isMExist) {
                    Description d = (Description) m.getAnnotation(Description.class);
                    System.out.println(d.value());
                }
            }
            //另外一种解析方法
            for (Method m : ms) {
                Annotation[] as = m.getAnnotations();
                for (Annotation a : as) {
                    //遍历查找Description的注解
                    if(a instanceof Description) {
                        Description d = (Description)a;
                        System.out.println(d.value());

                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
